import { clipboard } from '@ve/utils/clipboard'
import { jsonToTable } from '@ve/utils/jsonToTable'
//必须带done回调 执行完成调用
export default function copyTableRows({ vm, action, additionalData, done }) {
  let tableId = action.extension.tableId.value && action.extension.tableId.value[0]
  if (tableId && vm.vuiComponents) {
    let _table = vm.vuiComponents[tableId]
    if (_table) {
      let _table = vm.vuiComponents[tableId]
      let _value = _table.self.$getCheckboxOrRadioVals()

      let _tableColumns = additionalData.tableFun.getTableColumns

      let _header = {}
      _tableColumns.forEach((v) => {
        if (v.type !== 'checkbox' && v.type !== 'seq' && v._type !== 'operate') {
          _header[v.field] = v.title
        }
        if (v.type == 'seq') {
          _header['#SEQ#'] = v.title
        }
      })

      let _tableRows = []
      let rows = _value
      rows.forEach((v) => {
        let _row = {}
        Object.keys(_header).forEach((vv) => {
          _row[vv] = v[vv]
        })
        _tableRows.push(_row)
      })
      console.log('bb', _header, _tableRows)
      let tableTtml = jsonToTable({
        showHeader: action.extension.hasHeader.value || false,
        data: _tableRows,
        header_mapping: _header,
      })
      if (tableTtml) {
        clipboard(tableTtml)
      }
    } else {
      console.log('表格复制选中行没有找到表格')
    }
  }
  done(null, {})
}
